package com.tqz.rc.server.cluster;

import com.tqz.rc.common.util.EnvUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/9/24 10:18
 */
public class FileConfigManagerLookup {

    private final Logger log = LoggerFactory.getLogger(FileConfigManagerLookup.class);

    private ServiceMemberManager serviceMemberManager;

    public FileConfigManagerLookup() {
        serviceMemberManager = new ServiceMemberManager();
        init();
    }

    private void init() {
        readClusterConfFromDisk();
    }

    private void readClusterConfFromDisk() {
        Collection<Member> tmpMembers = new ArrayList<>();
        try {
            List<String> tmp = EnvUtil.readClusterConf();
            tmpMembers = MemberUtils.readServerConf(tmp);
        } catch (Throwable e) {
            log.error("tqzRc-XXXX [serverlist] failed to get serverlist from disk!, error : {}", e.getMessage());
        }

        afterLookup(tmpMembers);
    }

    private void afterLookup(Collection<Member> members) {
        serviceMemberManager.memberChange(members);
    }
}
